package com.oa.xinxi.biz;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.swing.JOptionPane;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.oa.office.util.UtilHelp;
import com.oa.xinxi.dao.DepMapper;
import com.oa.xinxi.dao.PPEMapper;
import com.oa.xinxi.entity.Dep;
import com.oa.xinxi.entity.PPE;
import com.oa.xinxi.utils.ExportExcel;
import com.oa.xinxi.utils.ReadExcel;
/**
 * @author xmp
 *固定资产Biz
 */
@Service
public class PPEBiz {
	@Resource
	private PPEMapper ppeMapper;
	@Resource
	private UtilHelp utilHelp;
	public void save(PPE ppe) {
		ppeMapper.insertSelective(ppe);
	}
	public void deleteById(Integer id) {
		ppeMapper.deleteByPrimaryKey(id);
	}
	public void update(PPE ppe) {
		ppeMapper.updateByPrimaryKeySelective(ppe);
	}
	public PPE findById(Integer id) {
		return ppeMapper.selectByPrimaryKey(id);
	}
	public List<PPE> findAll() {
		return ppeMapper.findAll();
	}
	public List<PPE> findByDepId(int id) {
		return ppeMapper.findByDepId(id);
	}
	public Integer findDepIdByName(String name) {
		return ppeMapper.findDepIdByName(name);
	}
	/**
	 * @param file
	 * @param userName
	 * @throws Exception
	 * 导入Excel
	 */
	public void insertExcel(File file, String userName) throws Exception {
		//读取excel
		List<List<Object>> list = ReadExcel.readExcel(file);
		//封装excel对象
		for (int i = 1; i < list.size(); i++) {
			List<Object> list2 = list.get(i);
			PPE ppe = new PPE();
			ppe.setName((String)list2.get(0));
			ppe.setModel((String)list2.get(1));
			String num = (String)list2.get(3);
			ppe.setNumber(Integer.valueOf(num.substring(0, num.lastIndexOf("."))));
			String num1 = (String)list2.get(2);
			ppe.setPrice(Double.valueOf(num1.substring(0, num1.lastIndexOf("."))));
			String num3 = (String)list2.get(4);
			ppe.setSumPrice(Double.valueOf(num3.substring(0, num1.lastIndexOf("."))));
			String str = (String)list2.get(5);
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
			java.util.Date date=sdf.parse(str.substring(0, str.lastIndexOf(" "))); 
			ppe.setBuyTime(new java.sql.Date(date.getTime()));
			ppe.setBaoxiu((String)list2.get(6));
			ppe.setSyqk((String)list2.get(7));
			ppe.setSyqkDetail((String)list2.get(8));
			ppe.setAddress((String)list2.get(9));
			ppe.setZerenren((String)list2.get(11));
			ppe.setCreatetime(new Date(new java.util.Date().getTime()));
			ppe.setCreator(userName);
			ppe.setDepId(ppeMapper.findDepIdByName((String)list2.get(10)));
			//持久化到数据库
			ppeMapper.insertSelective(ppe);
		}
	}
	/**
	 * @param list
	 * 导出为Excel
	 */
	public void transtoexcel(List<PPE> list) {
		ExportExcel<PPE> ex = new ExportExcel<PPE>();
		String[] headers = { "编号","物品名称", "型号", "数量", "单价", "总价", "购买时间","保修","使用情况","使用详情","存放地址","部门","责任人","创建人","创建时间"};
		//获取导出路径
		String path = utilHelp.getPath();
		OutputStream out;
		try {
			out = new FileOutputStream(path 
					+ "固定资产.xls");
			ex.exportExcel(headers, list, out);
			out.close();
			JOptionPane.showMessageDialog(null, "导出成功!");
			System.out.println("excel导出成功！");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
